/**
 * 
 */
package homework.homework5.problem1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author hoang.pt
 * 
 */
public class P1Demo {

	static ComputerList list = new ComputerList();
	static InputStreamReader reader = new InputStreamReader(System.in);
	static BufferedReader in = new BufferedReader(reader);

	
	/**
	 * constructor
	 */
	public P1Demo() {
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// display menu
		try {
			displayMenu();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * display Menu
	 * 
	 * @throws IOException
	 */
	public static void displayMenu() throws IOException {

		// init choice, not use scanner to read choice
		int choice;

		// loop until choice = 3
		do {
			// display menu
			System.out.println("Aprotrain-aptech lab on the fly");
			System.out.println("1. Manage computer");
			System.out.println("2. Manage teacher");
			System.out.println("3. Manage student");
			System.out.println("4. Exit");
			System.out.println("------------------");
			System.out.println("Enter your choice: ");

			// read choice
			String temp = in.readLine();
			choice = Integer.parseInt(temp);

			// switch choice
			switch (choice) {
			case 1:
				subMenu("computer");
				break;
			case 2:
				subMenu("teacher");
				break;
			case 3:
				subMenu("student");
				break;
			case 4:
				System.out.println("Bye bye.");
				System.exit(0);
			default:
				System.out.println("Invalid choice. Enter again.");
				break;

			}
		} while (choice != 4);

	}

	/**
	 * 
	 * @param param
	 * @throws IOException
	 */
	public static void subMenu(String param) throws IOException {
		int choice;

		// display menu
		System.out.println("1. Add " + param + ".");
		System.out.println("2. List " + param + ".");
		System.out.println("3. Search " + param + ".");
		System.out.println("4. Back to main menu");
		System.out.println("Enter your choice: ");

		// read choice
		String temp = in.readLine();
		choice = Integer.parseInt(temp);

		// switch choice
		switch (choice) {
		case 1:
			list.addComputer();
			break;
		case 2:
			list.displayAll();
			break;
		case 3:
			list.searchComputer();
			break;
		case 4:
			displayMenu();
			break;
		default:
			System.out.println("Invalid choice. Enter again.");
			break;

		}
	}
}
